package com.yidu.t299.dao.xp;
import com.yidu.t299.domain.xp.Class;
import com.yidu.t299.domain.xp.ClassManage;
import com.yidu.t299.domain.xp.School;
import com.yidu.t299.domain.xp.StudentManage;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import java.util.List;

/*
使用Mybatis逆向生成自动生成 后续自己更改
 */
@Mapper
public interface ClassManageMapper {
    /**
     * 根据报班编号删除
     * @param claId 报班编号
     * @return
     */
    @Delete({
        "delete from class_manage",
        "where cla_id = #{claId,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(Integer claId);

    /**
     * 增加报班信息
     * @param record 报班对象
     * @return 影响的行数(返回1则成功)
     */
    @Insert({
        "insert into class_manage (cla_id, class_id, ",
        "student_id, cla_dates)",
        "values (#{claId,jdbcType=INTEGER}, #{classId,jdbcType=INTEGER}, ",
        "#{studentId,jdbcType=INTEGER}, #{claDates,jdbcType=VARCHAR})"
    })
    int insert(ClassManage record);

    /**
     * 根据报班编号查询指定信息
     * @param claId 报班编号
     * @return
     */
    @Select({
        "select",
        "cla_id, class_id, student_id, cla_dates",
        "from class_manage",
        "where cla_id = #{claId,jdbcType=INTEGER}"
    })
    @Results(id = "ClassManageMap",value = {
        @Result(column="cla_id", property="claId", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="class_id", property="classId", jdbcType=JdbcType.INTEGER),
        @Result(column="student_id", property="studentId", jdbcType=JdbcType.INTEGER),
        @Result(column="cla_dates", property="claDates", jdbcType=JdbcType.VARCHAR),
    })
    ClassManage selectByPrimaryKey(Integer claId);

    /**
     * 按条件分页查询报班数据集合
     * @param rows 每页的行数
     * @param page 页码
     * @param condition 条件参数
     * @return 日志对象集合
     */
    @Select("SELECT * FROM class_manage cm " +
            "JOIN class c " +
            "ON cm.class_id=c.class_id " +
            "JOIN student_manage sm " +
            "ON cm.student_id=sm.student_id " +
            "JOIN school b " +
            "ON sm.school_id=b.school_id " +
            "where 1=1 ${condition} limit #{page},#{rows}")
    @Results({
            @Result(column = "cla_id",property = "claId", jdbcType = JdbcType.INTEGER, id = true),
            @Result(column = "class_id",property ="classId" ,jdbcType = JdbcType.INTEGER),
            @Result(column = "student_id",property ="studentId" ,jdbcType = JdbcType.INTEGER),
            @Result(column = "cla_dates",property ="claDates" ,jdbcType = JdbcType.VARCHAR),
            @Result(column = "student_name",property ="studentName" ,jdbcType = JdbcType.VARCHAR),
            @Result(column = "class_name",property ="className" ,jdbcType = JdbcType.VARCHAR),
            @Result(column = "school_name",property ="schoolName" ,jdbcType = JdbcType.VARCHAR)
    })
    List<ClassManage> selectByPage(@Param("rows") int rows, @Param("page") int page, @Param("condition") String condition);

    /**
     * 按条件查询class_manage表中数据行数,因为涉及了多表
     * 所以这里也需要多表联查不然无法设置条件
     * @param condition 条件参数
     * @return 数据行数
     */
    @Select("select count(*) from class_manage cm " +
            "JOIN class c " +
            "ON cm.class_id=c.class_id " +
            "JOIN student_manage sm " +
            "ON cm.student_id=sm.student_id " +
            "JOIN school b " +
            "ON sm.school_id=b.school_id " +
            "where 1=1 ${condition}")
    int selectCount(@Param("condition") String condition);

    /**
     * 根据报班信息修改其数据
     * @param record
     * @return
     */
    @Update({
        "update class_manage ",
        "set class_id = #{classId,jdbcType=INTEGER},",
          "student_id = #{studentId,jdbcType=INTEGER},",
          "cla_dates = #{claDates,jdbcType=VARCHAR}",
        " where cla_id = #{claId,jdbcType=INTEGER}"
    })
    int updateByPrimaryKey(ClassManage record);

    //以下是获取下拉框数据的方法

    /**
     * 得到所有学员姓名信息
     * @return 学员名称集合
     */
    @Select("select student_name from student_manage")
    @Results({
            @Result(id = true,column = "student_name",property = "studentName")
    })
    List<StudentManage> selectAllGetStudent();

    /**
     * 得到所有班级名称信息
     * @return 班级名称集合
     */
    @Select("select class_name from class")
    @Results({
          @Result(id = true,column = "class_name",property = "className")
    })
    List<Class> selectAllGetClass();

    /**
     * 得到所有校区名称信息
     * @return 校区名称集合
     */
    @Select("select school_name from school")
    @Results({
            @Result(id = true,column = "school_name",property ="schoolName")
    })
    List<School> selectAllGetSchool();

    /**
     *针对学员表(student_manage)进行操作
     * 根据学员名称查找其编号
     * @param studentName 学员名称
     * @return 影响的行数(>1:成功)
     */
    @Select("select student_id from student_manage where student_name=#{studentName}")
    int selectByStudentName(@Param("studentName") String studentName);

    /**
     * 针对班级表(class)进行操作
     * @param className 班级名称
     * @return 影响的行数(>1:成功)
     */
    @Select("select class_id from class where class_name=#{className}")
    int selectByclassName(@Param("className") String className);
}